
#include "stdafx.h"
#include "titlebar.h"
#include "resource.h"
#include "wallpaperhelper.hpp"
#include "global.h"

TitleBar::TitleBar(void)
	: ImageCtrlBox()
{
}

TitleBar::~TitleBar(void)
{
}

void TitleBar::Load()
{
	m_resetctrl.Load(
		&ImageCtrl::ResInfo::MakeResInfo(IDR_RESET_NORMAL, L"png", CXIMAGE_FORMAT_PNG),
		&ImageCtrl::ResInfo::MakeResInfo(IDR_RESET_HOVER, L"png", CXIMAGE_FORMAT_PNG),
		&ImageCtrl::ResInfo::MakeResInfo(IDR_RESET_DOWN, L"png", CXIMAGE_FORMAT_PNG));
	m_minctrl.Load(
		&ImageCtrl::ResInfo::MakeResInfo(IDR_MIN_NORMAL, L"png", CXIMAGE_FORMAT_PNG),
		&ImageCtrl::ResInfo::MakeResInfo(IDR_MIN_HOVER, L"png", CXIMAGE_FORMAT_PNG),
		&ImageCtrl::ResInfo::MakeResInfo(IDR_MIN_DOWN, L"png", CXIMAGE_FORMAT_PNG));
	m_closectrl.Load(
		&ImageCtrl::ResInfo::MakeResInfo(IDR_CLOSE_NORMAL, L"png", CXIMAGE_FORMAT_PNG),
		&ImageCtrl::ResInfo::MakeResInfo(IDR_CLOSE_HOVER, L"png", CXIMAGE_FORMAT_PNG),
		&ImageCtrl::ResInfo::MakeResInfo(IDR_CLOSE_DOWN, L"png", CXIMAGE_FORMAT_PNG));

	m_resetctrl.SetCallback(this);
	m_minctrl.SetCallback(this);
	m_closectrl.SetCallback(this);

	m_imagectrls.push_back(&m_resetctrl);
	m_imagectrls.push_back(&m_minctrl);
	m_imagectrls.push_back(&m_closectrl);

	Layout();
}

void TitleBar::Layout()
{
	RECT rect;

	rect.right = m_rect.right - 4;
	rect.left = rect.right - m_closectrl.GetImageSize().cx,
	rect.top = 4;
	rect.bottom = 4 + m_closectrl.GetImageSize().cy;
	m_closectrl.SetRect(rect);

	rect.right = rect.left - 4;
	rect.left = rect.right - m_minctrl.GetImageSize().cx,
	rect.top = 4;
	rect.bottom = 4 + m_minctrl.GetImageSize().cy;
	m_minctrl.SetRect(rect);

	rect.right = rect.left - 4;
	rect.left = rect.right - m_resetctrl.GetImageSize().cx,
	rect.top = 4;
	rect.bottom = 4 + m_resetctrl.GetImageSize().cy;
	m_resetctrl.SetRect(rect);
}

void TitleBar::OnClick(ImageCtrl* imagectrl)
{
	if (imagectrl == &m_resetctrl)
	{
		WallpaperHelper::instance().RestoreWallpaper();
	}
	else if (imagectrl == &m_minctrl)
	{
		ShowWindow(Global::instance().hwnd, SW_MINIMIZE);
	}
	else if (imagectrl == &m_closectrl)
	{
		SendMessage(Global::instance().hwnd, WM_CLOSE, 0, 0);
	}
}
